package com.zyf.controller.api;


import com.zyf.common.excption.YyghException;
import com.zyf.common.helper.HttpRequestHelper;
import com.zyf.common.result.Result;
import com.zyf.common.result.ResultCodeEnum;
import com.zyf.common.util.MD5;
import com.zyf.model.hosp.Schedule;
import com.zyf.service.HospitalSetService;
import com.zyf.service.ScheduleService;
import com.zyf.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author zyf
 * @date 2021年05月08日 14:46
 */
@RestController
@RequestMapping("/api/hosp/schedule")
@Api(tags = "医院排班信息管理")
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "上传医院排班信息Api")
    @ApiImplicitParam(name = "request", value = "医院排班信息Map")
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        //获取传递过来的医院科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //1、获取医院系统传递过来的签名，  签名进行过MD5加密
        //必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        String hospSign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);

        String signKeyMD5 = MD5.encrypt(signKey);

        if (!hospSign.equalsIgnoreCase(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service里的方法
        scheduleService.saveSchedule(paramMap);

        return Result.ok();
    }

    @ApiOperation(value = "分页查询医院排班信息Api")
    @ApiImplicitParam(name = "request", value = "医院排班信息Map")
    @PostMapping("/list")
    public Result findScheduleByPage(HttpServletRequest request) {
        //获取传递过来的医院科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //1、获取医院系统传递过来的签名，  签名进行过MD5加密
        //必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        String hospSign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);

        String signKeyMD5 = MD5.encrypt(signKey);

        if (!hospSign.equalsIgnoreCase(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service里的方法
        //获取分页参数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));

        ScheduleQueryVo queryVo = new ScheduleQueryVo();
        queryVo.setHoscode(hoscode);

        Page<Schedule> schedule = scheduleService.findScheduleByPage(page, limit, queryVo);

        return Result.ok(schedule);
    }

    @ApiOperation(value = "根据医院编号和排班编号删除医院排班API")
    @ApiImplicitParam(name = "request", value = "医院排班信息Map")
    @PostMapping("/remove")
    public Result removeSchedule(HttpServletRequest request) {
        //获取传递过来的医院科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //1、获取医院系统传递过来的签名，  签名进行过MD5加密
        //必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        String hospSign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);

        String signKeyMD5 = MD5.encrypt(signKey);

        if (!hospSign.equalsIgnoreCase(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //获取排班编号
        String hosScheduleId = (String) paramMap.get("hosScheduleId");

        scheduleService.removeSchedule(hoscode, hosScheduleId);
        return Result.ok();
    }
}
